<template>
  <view class="page" :style="{'overflow':show?'hidden':''}">
    <scroll-view scroll-y="true" class="scroll-Y" :key="1" v-if="!show">
      <view class="context">
        <view class="title">
          <text>不知道写什么？试试这个</text>
          <image style="width: 48rpx;height: 24rpx;margin: 0 4px;" src="/static/create/create_finger.png"></image>
          <text style="color: #f1445d" @click="select">春天的故事</text>
        </view>

     <view class="context-tip">
       <view class="context-tip-left">
         <text>扩写内容</text>
         <text class="context-tip-left-tip">*</text>
       </view>
       <!--            <view class="context-tip-right">至少包含50个字</view>-->
     </view>
     <view class="context-textarea">
       <textarea :value="writing" placeholder="请输入您想要的内容关键词。内容越详细越准确，生成的内容越好哦！"
         placeholder-style="color:#999999;font-size:24rpx" @input="changeValue" :maxlength="600"
         class="context-textarea-input"></textarea>
       <view class="context-textarea-max">{{writing.length}}/600</view>
     </view>
		<view class="context-tip">
		  <view class="context-tip-left">
		    <text>请输入扩写字数</text>
		    <text class="context-tip-left-tip">*</text>
		  </view>
		  <view class="context-tip-right"></view>
		</view>
		<view class="context-textarea" style="height: 80rpx">
		  <textarea :value="title"  placeholder-style="color:#999999;font-size:24rpx"
		    :maxlength="4" @input="changeTitle" style="margin-top: -9rpx" class="context-textarea-input"></textarea>
		  <!-- <view class="context-textarea-max">{{title.length}}/50</view> -->
		</view>
        <view class="button" @click="create"
          :style="{'background':(writing.length>0)?'linear-gradient(180deg, #E9556A 0%, #F97A8C 100%);':'#FFEEF0'}">
          开始生成</view>
        <view style="height: 80rpx"></view>
      </view>
    </scroll-view>
    <view class="page-view" v-if="show" :key="2">
      <view class="answer">
        <view class="answer-value">{{titles}}</view>
        <view class="answer-textarea">
          <textarea :value="creativeResult" :maxlength="-1" class="answer-textarea-input"></textarea>
          <view class="answer-fontLength">共{{creativeResult.length}}字</view>
        </view>
      </view>
      <view class="tabbar">
        <view class="tabbar-item" @click="clickTab(item,index)" v-for="(item, index) in tabbarList" :key="index">
          <view>
            <image style="width: 38rpx;height: 38rpx;margin-top:8rpx" :src="item.src"></image>
          </view>
          <view style="margin-left:30rpx">{{item.text}}</view>
        </view>
      </view>
    </view>
    <!--  进度条  -->
    <view class="schedule-page" v-if="scheduleShow">
      <view class="schedule-box">
        <view class="schedule-text">{{text}}</view>
        <view class="lineBox">
          <view class="lineAnimation" v-for="(item,index) in arrIndex" :key="index" :style="{'width':item.width+'%'}">
            <view v-if="item.widthChildren>0" class="lineAnimation-children" :style="{'width':item.widthChildren+'%'}">
            </view>
          </view>
        </view>
        <view class="schedule-number">{{schedule}}%</view>
      </view>
    </view>
  </view>
</template>
<script>
import {
  wycUrl,
  sslbiz,
  wycUrlApi
} from '@/api/assemb.js'
export default {
  data () {
    return {
		titles:'',
		content:'',
		toneItem:'',
      show: false,
      message: '',
      styleItem: '',
      writing: '',
      title: '',
      tabbarList: [

        {
          src: '/static/create/create_continue1.png',
          text: '重新生成'
        },
        {
          src: '/static/create/create_copy1.png',
          text: '复制文本'
        }
      ],
      creativeResult: '',
      creativeResults: '',
      compareResults: '',
      originality: '',
      aiLevel: 0,
      width: 0,
      arrIndex: [],
      widthIndex: 0,
      schedule: 0,
      text: '创作中,请稍后',
      scheduleShow: false,
	  isVip:false,
	  id:'',
    }
  },
  onShow () {
    this.isVip = uni.getStorageSync('vipType').vipStatus == 1 ? true : false;
  },
  onLoad (option) {
	  // this.id = option.id
	  // this.content = option.content
	  // this.titles = option.title
	  //赋值导航栏名称
	  uni.setNavigationBarTitle({
		  title:option.footer
	  })
	  console.log(option)
  },
  methods: {
    //进度条
    change () {
      clearTimeout(this.time);
      clearTimeout(this.time1);
      clearTimeout(this.times);
      var settext = [
        "创作中,请稍后",
        "优化中,请稍后",
        "下载中,请稍后",
        "合并中,请稍后",
        "上传中,请稍后"
      ];
      let inx = 0;
      this.widthIndex = 0;
      this.schedule = 0;
      this.arrIndex = this.arrIndex.map(item => {
        return {
          ...item,
          width: 100 / (this.arrIndex.length),
          widthChildren: 0
        };
      });

      // 计算总进度
      let sum = () => {
        let number = this.arrIndex
          .map((item) => item.widthChildren)
          .reduce((accumulator, currentValue) => accumulator + currentValue);
        this.schedule = Math.floor(number / this.arrIndex.length);
      };
      const loadNextProgress = () => {
        if (this.widthIndex >= this.arrIndex.length) {
          this.time = setTimeout(() => {
            this.widthIndex = 0; // 重置为0，从新循环
            console.log('下一个进度条')
            loadNextProgress();
          }, 100); // 等待 1s 后重新开始加载
          if (this.widthIndex >= this.arrIndex.length - 1) {
            this.arrIndex.forEach((item) => {
              item.widthChildren = 0;
            });
            inx += 1;
            if (inx > 4) {
              this.text = settext[4]
            } else {
              this.text = settext[inx]
            }
            this.schedule = 0;
            clearTimeout(this.time)
            this.time = null
          }
          this.time1 = setTimeout(() => {
            this.widthIndex = 0;
            loadNextProgress();
            clearTimeout(this.time1)
          }, 300);
          return;
        }
        if (this.widthIndex > 0) {
          // 如果是加载了第二个进度，则让上一个进度条伪 100%
          this.arrIndex[this.widthIndex - 1].widthChildren = 100;
        }
        if (this.arrIndex[this.widthIndex].widthChildren < 99) {
          this.arrIndex[this.widthIndex].widthChildren++;
          this.times = setTimeout(loadNextProgress, 300); // 间隔 100ms 执行下一次加载
        } else {
          this.widthIndex++; // 接着下一个进度跑
          loadNextProgress();
        }
        sum(); // 计算整体的进度值
      };
      loadNextProgress();
    },
  async original(param, i) {
  	let arr = []
  	arr.push(this.arrIndex);
  	this.arrIndex = arr
  	console.log(this.arrIndex)
  	this.scheduleShow = true
  	this.text = '创作中,请稍后'
  	this.change()
  	await wycUrl(param, 1009, 'magicChat').then((res) => {
  		if (res.data.code == 200) {
  			this.show = true
  			this.scheduleShow = false
  			this.creativeResult += res.data.data.results[0]
  			if (!this.isVip) {
  				console.log('走了吗')
  				let param = {
  					consumecoins: 1,
  					descinfo: '一键创作消耗',
  					consumetype: 1,
  				}
  				apiMixUrlApi(param, '1043', "ConsumeCoins").then(result => {
  					console.log(result, '1043返回值');
  					uni.setStorageSync('frequencyrs', result.coins)
  				});
  			}
  		} else {
  			uni.showModal({
  				title: '提示',
  				content: res.data.message
  			})
  			this.scheduleShow = false
  		}
  	}).catch(err => {
  		console.log('出现错误')
  		this.scheduleShow = false
  		uni.showToast({
  			title: '系统繁忙,请稍后重试',
  			icon: 'none'
  		})
  	})
  },
    select () {
           this.title = 800
           this.writing = '春天来了 花儿开了'
	  
	
    },
    changeTitle(e){
      this.title = e.detail.value
    },
    changeValue (e) {
      this.writing = e.detail.value
    },
   async create() {
   	if (!this.writing) {
   		uni.showToast({
   			title: '请输入关键词',
   			icon: 'none',
   			duration: 2000
   		});
   		return
   	}
	if (!this.title) {
		uni.showToast({
			title: '请输入字数',
			icon: 'none',
			duration: 2000
		});
		return
	}
   let param = {
   "promptId":220,
   "content":this.writing,
    "conditions": [ this.title]
   }
   	if (uni.getStorageSync('frequencyrs') > 0 || this.isVip) {
   		this.original(param);
   	} else {
   		uni.showModal({
   			title: '提示',
   			content: '体验次数用完,请开通会员',
   			success: function(res) {
   				if (res.confirm) {
   					if (uni.getStorageSync('OS') !== 'ios') {
   						uni.navigateTo({
   							url: '/pages/paymentCenter/paymentCenter'
   						});
   					} else {
   						if (getApp().globalData.iosPay == 0) {
   							console.log(getApp().globalData.iosPay)
   							//跳转网页充值
   							uni.navigateTo({
   								url: '/pages/goods/goods'
   							});
   						} else if (getApp().globalData.iosPay == 1) {
   							console.log(getApp().globalData.iosPay)
   							//跳转关注公众号充值
   							uni.navigateTo({
   								url: '/pages/webview/view'
   							});
   						} else if (getApp().globalData.iosPay == 2) {
   							//跳转扫码联系客服充值
   							uni.navigateTo({
   								url: '../service/service'
   							});
   						} else if (getApp().globalData.iosPay == 3) {
   							uni.navigateTo({
   								url: '/pages/goods/goodr'
   							});
   						}
   					}
   				} else if (res.cancel) {
   					console.log('用户点击取消');
   				}
   			}
   		});
   	}
   
   },
    clickTab (item, index) {
      if (index == 0) {
        this.show = false
      } else {
        let text = this.creativeResult
        // #ifdef H5
        let textarea = document.createElement("textarea")
        textarea.value = text
        textarea.readOnly = "readOnly"
        document.body.appendChild(textarea)
        textarea.select() // 选中文本内容
        textarea.setSelectionRange(0, text.length)
        uni.showToast({//提示
          title: '复制成功',
          icon: 'success'
        })
        document.execCommand("copy")
        textarea.remove()
        // #endif
        // #ifndef H5
        uni.setClipboardData({
          data: text,//要被复制的内容
          success: () => {//复制成功的回调函数
            uni.showToast({//提示
              title: `复制成功`,
              icon: 'success'
            })
          }
        }, true);
        // #endif

      }
    },
  },
}
</script>
<style lang="less">
.page {
  width: 100%;
  height: calc(100vh);
 background: #FDFDFD;
}
.scroll-Y {
  width: 100%;
  height: 100%;
}
.page-view {
  width: 100%;
  height: 100%;
}
.title {
  width: 100%;
  height: 80rpx;
  margin: 0 0 0 -20px;
  box-sizing: border-box;
  padding: 0rpx 40rpx;
  display: flex;
  align-items: center;
  //   background-color: #F7F7F7;
  font-size: 24rpx;
  color: #666666;
}
.context {
  width: 90%;
  margin: 0rpx auto;
  .context-tone {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .context-tone-item {
      width: 18%;
      padding: 12rpx 14rpx;
      // background-color: #F7F7F7;
      border: 1px solid #FFEEF0;
      border-radius: 24rpx;
      font-size: 22rpx;
	  background: #F8F8F8;
      //   margin-left: 15rpx;
      //   margin-right: 30rpx;
      //   margin-bottom: 30rpx;
      margin: 5px 0 0 8.3px;
      color: #333333;
      text-align: center;
    }
    .context-tone-item-select {
      //   background-color: #4BB591;
      background-color: #FFEEF0;
      color:#F1445D;
    }
  }
  .context-tip {
    width: 100%;
    height: 90rpx;
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .context-tip-left {
      font-size: 30rpx;
      color: #333333;
      display: flex;
      align-items: center;
      /deep/.context-tip-left-tip span {
        color: red;
        margin-left: 6rpx;
        vertical-align: sub;
      }
    }
    .context-tip-right {
      color: #999999;
      font-size: 24rpx;
    }
  }
  .context-title {
    width: 100%;
    height: 90rpx;
    font-size: 30rpx;
    color: #333333;
    margin-top: 10px;
    display: flex;
    align-items: center;
    /deep/.context-title-tip span {
      color: red;
      margin-left: 6rpx;
      vertical-align: sub;
    }
  }
  .context-textarea {
    width: 100%;
    height: 240rpx;
    // background-color: #F7F7F7;
    // border: 1px solid #FFEEF0;
    border-radius: 24rpx;
    position: relative;
    .context-textarea-input {
      width: 100%;
      height: 100%;
      box-sizing: border-box;
      padding:14rpx;
      color: #333333;
      font-size: 28rpx;
	  background: #F8F8F8;
    }
    .context-textarea-max {
      position: absolute;
      right: 40rpx;
      bottom: 20rpx;
      color: #999999;
    }
  }
  .button {
    width: 100%;
    height: 90rpx;
    background-color: rgba(52, 116, 255, 0.3);
    border-radius: 60rpx;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 30rpx;
    margin: 40rpx auto 30rpx;
  }
}
.btn {
  display: flex;
  position: fixed;
  flex-direction: row;
  border-radius: 25px 25px 0 0;
  width: 100%;
  bottom: 0px;
  z-index: 99;
  margin-left: -20px;
}
.placeholder {
  height: 50%;
  padding-bottom: 200rpx;
}
.answer {
  width: 90%;
  margin: 0px auto;
  height: 90%;
  .answer-fontLength {
    width: 100%;
    height: 60rpx;
    color: #999999;
    font-size: 30rpx;
    // display: flex;
    // align-items: center;
    //padding-left: 520rpx;
    position: absolute;
    left: 76%;
  }
  .answer-value {
    width: 100%;
    height: 100rpx;
    color: #333333;
    font-size: 44rpx;
    line-height: 100rpx;
    // display: flex;
    // align-items: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .answer-textarea {
    position: relative;
    width: 100%;
    height:85%;
    // background-color: #F7F7F7;
    border: 1px solid #FFEEF0;
    border-radius: 24rpx;
    .answer-textarea-input {
      width: 100%;
      height: 92%;
      box-sizing: border-box;
      padding: 30rpx 30rpx 20rpx;
      color: #333333;
      font-size: 28rpx;
    }
  }
}
.tabbar {
  width: 100%;
  //   height: 10%;
  //   background-color: #fff;
  display: flex;
  box-sizing: border-box;
  justify-content: center;
  //   border-top: 1rpx solid #e5dbdb;
  .tabbar-item {
    width: 33.33%;
    height: 68.84rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    // flex-direction: column;
    font-family: PingFang SC;
    padding: 0 40rpx;
    font-weight: 500;
    color:#fff;
    line-height: 36rpx;
    font-size: 24rpx;
    border: solid 2rpx;
    margin: 0 12rpx;
   background: linear-gradient(180deg, #E9556A 0%, #F97A8C 100%);
    border-radius: 34rpx;
  }
}

/deep/::-webkit-scrollbar {
  display: none;
  width: 0 !important;
  height: 0 !important;
  -webkit-appearance: none;
  background: transparent;
  color: transparent;
}

.schedule-page {
  width: 100%;
  height: 100vh;
  position: fixed;
  background-color: rgba(128, 126, 126, 0.8);
  top: 0px;
  left: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 200;
}
.schedule-box {
  width: 62%;
  background-color: #fff;
  border-radius: 15rpx;
}
.schedule-text {
  text-align: center;
  line-height: 120rpx;
}
.schedule {
  width: 100%;
  height: 20rpx;
  margin: 20rpx 0rpx;
}
.schedule-number {
  width: 100%;
  height: 100rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #2979ff;
}
.lineBox {
  width: 90%;
  display: flex;
  justify-content: space-around;
  margin: 0px auto;
  height: 12rpx;
  border-radius: 24rpx;
  overflow: hidden;
}
.lineAnimation {
  height: 100%;
  background-color: #bbb9b9;
}
.lineAnimation-children {
  height: 100%;
  transition: all 0.5s;
  background-color: #2979ff;
}
.baifenbizdy {
  color: skyblue;
  font-size: 22px;
  align-items: center;
  justify-content: center;
  display: flex;
  margin-top: 7%;
}
.isshow {
  background-color: #fff;
  border-radius: 10px;
  width: 90%;
  height: 190px;
  left: 5%;
  position: relative;
  top: -70%;
  font-size: 50px;
  z-index: 10086;
  color: #1890ff;
  padding: 40px 0 0 0;
}
.box {
  width: 880px;
  background: #ffffff;
  position: relative;
  // height: 640px;
}
.textzdy {
  display: flex;
  font-size: 22px;
  margin: auto;
  color: grey;
  align-items: center;
  justify-content: center;
  padding-top: 2%;
}
.container {
  width: 90%;
  height: 7px;
  background-color: #fafafc;
  border-radius: 15px;
  margin-left: 5%;
  margin-top: 8%;
}
/* 内部加载部分样式 */
.contentzdy {
  width: 100%;
  height: 7px;
  border-radius: 15px;
  background-color: #1890ff;
  text-align: center;
  line-height: 10px;
}
.jindu {
  margin-top: 1px;
}
</style>